package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dao.AddressBookDao;
import com.itheima.pojo.ShoppingCart;
import com.itheima.pojo.User;
import com.itheima.service.ShoppingCartService;
import com.itheima.service.UserService;
import com.itheima.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart,HttpServletRequest request){

        ShoppingCart one = shoppingCartService.add(shoppingCart);
        return R.success(one);
       /* //获取用户id
        Long userId = (Long) request.getSession().getAttribute("user");
//        菜品或者套餐是否点过
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        if(shoppingCart.getDishId() != null){    //点的是菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else { //点的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one == null){    //第一次点，直接添加到购物车
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else { //点过，增加数量
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }
        //获取添加到数据库中数据
        ShoppingCart one1 = shoppingCartService.getOne(wrapper);
        return R.success(one1);*/
    }


    /**
     * 查询购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> selectShoppingCart(){
        Long userId = (Long) BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 取消购物车
     */

    @PostMapping("/sub")
    public R<ShoppingCart> cancelShoppingCat(@RequestBody ShoppingCart shoppingCart){
       /* //获取用户id
        Long userId = (Long) request.getSession().getAttribute("user");
//        菜品或者套餐是否点过
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        if(shoppingCart.getDishId() != null){    //点的是菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else { //点的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if(number > 0){
            shoppingCartService.updateById(one);
        }else { //
            shoppingCartService.remove(wrapper);
        }*/
        ShoppingCart one = shoppingCartService.cancelShoppingCat(shoppingCart);
        return R.success(one);
    }

    /**
     * 清空购物车
     */

    @DeleteMapping("/clean")
    public R<String> cleanShoppingCart(HttpServletRequest request){
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }
}
